package com.zx.servicelearn.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

import com.zx.servicelearn.db.MySQLiteOpenHelper;

import static com.zx.servicelearn.db.MySQLiteOpenHelper.BOOK_TABLE_NAME;
import static com.zx.servicelearn.db.MySQLiteOpenHelper.USER_TABLE_NAME;

public class BookProvider extends ContentProvider {
    public final String TAG = getClass().getSimpleName();

    public static final String AUTHORITY = "com.zx.servicelearn.book.provider";
    public static final Uri BOOK_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/book");
    public static final Uri USER_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/user");

    private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

    static {
        sUriMatcher.addURI(AUTHORITY, "book", 0);
        sUriMatcher.addURI(AUTHORITY, "user", 1);
    }

    public BookProvider() {
        Log.e(TAG, "BookProvider:");
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        Log.e(TAG, "delete:");
        // Implement this to handle requests to delete one or more rows.
        return 0;
    }

    @Override
    public String getType(Uri uri) {
        Log.e(TAG, "getType:");
        // at the given URI.
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        Log.e(TAG, "insert:");
        String table = getTableName(uri);
        if (table == null) {
            throw new IllegalArgumentException("UnSupported uri:" + uri);
        }
        DB.insert(table, null, values);
        getContext().getContentResolver().notifyChange(uri, null);//?
        return uri;
    }

    SQLiteDatabase DB;

    @Override
    public boolean onCreate() {
        Log.e(TAG, "onCreate:current thread:" + Thread.currentThread());
        // TODO: 2018/1/31
        createDB();
        return false;
    }

    private void createDB() {
        DB = new MySQLiteOpenHelper(getContext()).getWritableDatabase();

        String del1 = "delete from " + BOOK_TABLE_NAME;
        String del2 = "delete from " + USER_TABLE_NAME;
        DB.execSQL(del1);
        DB.execSQL(del2);
        String sql = "insert into " + BOOK_TABLE_NAME + " values(1,'Android')";
        String sql2 = "insert into " + BOOK_TABLE_NAME + " values(2,'IOS')";
        String sql3 = "insert into " + BOOK_TABLE_NAME + " values(3,'JAVA')";
        String sql4 = "insert into " + BOOK_TABLE_NAME + " values(4,'C++')";
        DB.execSQL(sql);
        DB.execSQL(sql2);
        DB.execSQL(sql3);
        DB.execSQL(sql4);
        String sql5 = "insert into " + USER_TABLE_NAME + " values(3,'张三丰',1)";
        String sql6 = "insert into " + USER_TABLE_NAME + " values(4,'灭绝师太',0)";
        DB.execSQL(sql5);
        DB.execSQL(sql6);
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        Log.e(TAG, "query:current thread:" + Thread.currentThread());
        String table = getTableName(uri);
        if (table == null) {
            throw new IllegalArgumentException("UnSupported uri:" + uri);
        }
        return DB.query(table, projection, selection, selectionArgs, null, null, sortOrder, null);
    }

    private String getTableName(Uri uri) {
        String tableName = null;
        switch (sUriMatcher.match(uri)) {
            case 0:
                tableName = BOOK_TABLE_NAME;
                break;
            case 1:
                tableName = USER_TABLE_NAME;
                break;
            default:
                break;
        }
        return tableName;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        Log.e(TAG, "update:");
        return 0;
    }
}
